<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>缺陷管理</span>
        </div>
      </template>

      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="缺陷代码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入缺陷代码" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="缺陷名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入缺陷名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="缺陷类型" prop="defectType">
          <el-input v-model="queryParams.defectType" placeholder="请输入缺陷类型" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="缺陷等级" prop="defectLevel">
          <el-input v-model="queryParams.defectLevel" placeholder="请输入缺陷等级" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 200px">
            <el-option label="有效" value="1" />
            <el-option label="无效" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['qep:defect:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['qep:defect:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['qep:defect:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="defectList"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="缺陷代码" align="center" prop="code" />
        <el-table-column label="缺陷名称" align="center" prop="name" show-overflow-tooltip />
        <el-table-column label="缺陷类型" align="center" prop="defectType" />
        <el-table-column label="缺陷等级" align="center" prop="defectLevel" />
        <el-table-column label="描述" align="center" prop="description" show-overflow-tooltip />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '有效' : '无效' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button 
              link type="primary" 
              icon="Edit" 
              @click="handleUpdate(scope.row)"
              v-hasPermi="['qep:defect:edit']"
            >修改</el-button>
            <el-button 
              link type="primary" 
              icon="Plus" 
              @click="handleAddChild(scope.row)"
              v-hasPermi="['qep:defect:add']"
            >添加子项</el-button>
            <el-button 
              link type="primary" 
              icon="Delete" 
              @click="handleDelete(scope.row)"
              v-hasPermi="['qep:defect:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加或修改缺陷对话框 -->
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="defectForm" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="上级缺陷" v-if="form.parentId !== 0">
            <tree-select
              v-model="form.parentId"
              :options="defectOptions"
              :objMap="{ value: 'id', label: 'name', children: 'children' }"
              placeholder="选择上级缺陷"
            />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="缺陷代码" prop="code">
                <el-input v-model="form.code" placeholder="请输入缺陷代码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="缺陷名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入缺陷名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="缺陷类型" prop="defectType">
                <el-input v-model="form.defectType" placeholder="请输入缺陷类型" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="缺陷等级" prop="defectLevel">
                <el-select v-model="form.defectLevel" placeholder="请选择缺陷等级" style="width: 100%">
                  <el-option label="严重" value="严重" />
                  <el-option label="较重" value="较重" />
                  <el-option label="一般" value="一般" />
                  <el-option label="轻微" value="轻微" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="排序" prop="orderNum">
                <el-input-number v-model="form.orderNum" :min="1" :max="999" style="width: 100%" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio :label="1">有效</el-radio>
                  <el-radio :label="0">无效</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="缺陷描述" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入缺陷描述" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { listDefect, listDefectTree, getDefect, addDefect, updateDefect, delDefect } from '@/api/qep/inspection/defect';
import TreeSelect from '@/components/TreeSelect';

const defectList = ref([]);
const defectOptions = ref([]);
const showSearch = ref(true);
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const ids = ref([]);
const open = ref(false);
const title = ref('');

// 查询参数
const queryParams = reactive({
  code: undefined,
  name: undefined,
  defectType: undefined,
  defectLevel: undefined,
  status: undefined
});

// 表单参数
const form = reactive({
  id: undefined,
  parentId: 0,
  code: undefined,
  name: undefined,
  defectType: undefined,
  defectLevel: undefined,
  description: undefined,
  status: 1,
  orderNum: 1,
  remark: undefined
});

// 表单校验规则
const rules = reactive({
  code: [{ required: true, message: '缺陷代码不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '缺陷名称不能为空', trigger: 'blur' }],
  defectType: [{ required: true, message: '缺陷类型不能为空', trigger: 'blur' }],
  defectLevel: [{ required: true, message: '缺陷等级不能为空', trigger: 'change' }]
});

/** 查询缺陷列表 */
function getList() {
  loading.value = true;
  listDefectTree(queryParams).then(response => {
    defectList.value = response.data;
    loading.value = false;
  });
}

/** 查询缺陷下拉树结构 */
function getTreeselect() {
  listDefectTree().then(response => {
    defectOptions.value = response.data;
  });
}

/** 重置操作表单 */
function reset() {
  form.id = undefined;
  form.parentId = 0;
  form.code = undefined;
  form.name = undefined;
  form.defectType = undefined;
  form.defectLevel = undefined;
  form.description = undefined;
  form.status = 1;
  form.orderNum = 1;
  form.remark = undefined;
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.code = undefined;
  queryParams.name = undefined;
  queryParams.defectType = undefined;
  queryParams.defectLevel = undefined;
  queryParams.status = undefined;
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getTreeselect();
  open.value = true;
  title.value = "添加缺陷";
}

/** 新增子缺陷操作 */
function handleAddChild(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.parentId = row.id;
  }
  open.value = true;
  title.value = "添加子缺陷";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getTreeselect();
  if (row != null) {
    form.id = row.id;
    form.parentId = row.parentId;
    form.code = row.code;
    form.name = row.name;
    form.defectType = row.defectType;
    form.defectLevel = row.defectLevel;
    form.description = row.description;
    form.status = row.status;
    form.orderNum = row.orderNum;
    form.remark = row.remark;
  }
  open.value = true;
  title.value = "修改缺陷";
}

/** 提交按钮 */
function submitForm() {
  if (form.id !== undefined) {
    updateDefect(form).then(response => {
      ElMessage.success("修改成功");
      open.value = false;
      getList();
    });
  } else {
    addDefect(form).then(response => {
      ElMessage.success("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const defectIds = row.id || ids.value;
  ElMessageBox.confirm(
    '是否确认删除缺陷编号为"' + defectIds + '"的数据项?',
    "警告",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(function() {
    return delDefect(defectIds);
  }).then(() => {
    getList();
    ElMessage.success("删除成功");
  });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

onMounted(() => {
  getList();
});
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-table {
  margin-top: 15px;
}
</style> 